﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using System.Text;

using Frame.Lib.Core.Tree.Dir;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Tree;
using Frame.Lib.Common;
using Frame.Lib.Core.Right;
using Frame.Lib.Enums;



public partial class DocManger_SelectDirDlg : System.Web.UI.Page
{
    private List<int> lstSelectIDs;
    private DirBO dir = new DirBO("DocDir");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strSelectID = Request["SelectID"];
            if (strSelectID != null && strSelectID != "")
            {
                lstSelectIDs = StrUtil.SplitToInt(strSelectID);
            }
            tvList.Attributes.Add("onclick", "SingleSelect()");
            BindTree();
            TreeBO<TreeInfo>.SetSelect(tvList, lstSelectIDs);
        }
    }
    private void BindTree()
    {
        if (AppSession.UserID == Constants.ADMIN_ID)
        {
            dir.BindTreeView(tvList,null);
        }
        else
        {
            RecordRightBO right = new RecordRightBO("DocDirRight");
            List<int> lstDirIDs = right.GetBindIDs(AppSession.UserID, RecordRightName.Browse, RightOwnerType.User);
            dir.BindTreeView(tvList, lstDirIDs);

            //对有增加权限的加上CheckBox
            List<int> lstAddIDs = right.GetBindIDs(AppSession.UserID, RecordRightName.Add, RightOwnerType.User);
            if (tvList.Nodes.Count > 0)
            {
                SetCheckBox(tvList.Nodes[0], lstAddIDs);
            }
        }
    }
    private void SetCheckBox(TreeNode treeNode, List<int> lstAddIDs)
    {
        int id = int.Parse(treeNode.Value);
        if (lstAddIDs.IndexOf(id) >= 0)
        {
            treeNode.ShowCheckBox = true;
        }
        else
        {
            treeNode.ShowCheckBox = false;
        }
        foreach (TreeNode node in treeNode.ChildNodes)
        {
            SetCheckBox(node, lstAddIDs);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strValues = "";//选中的ＩＤ
        string strTexts = "";//选中的name

        TreeNodeCollection nodeCollection = tvList.CheckedNodes;

        foreach (TreeNode node in nodeCollection)
        {
            strValues += "," + node.Value;
            strTexts += "," + node.Text;
        }
        if (strValues != "")
        {
            strValues = strValues.Substring(1);
        }
        if (strTexts != "")
        {
            strTexts = strTexts.Substring(1);
        }

        StringBuilder sbJS = new StringBuilder();
        sbJS.Append("<script language='javascript'>\n");
        sbJS.Append("var args=window.dialogArguments;");
        sbJS.Append("args[0]='" + strValues + "';\n");
        sbJS.Append("args[1]='" + strTexts + "';\n");
        sbJS.Append("window.close();\n");
        sbJS.Append("</script>");
        Response.Write(sbJS.ToString());
        Response.End();
    }
}
